package com.echronos.payment.commons.cpcn.util;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.echronos.commons.utils.CopyObjectUtils;
import com.echronos.payment.util.ImageBase64Utils;
import payment.api.system.Gateway4FileEnvironment;
import payment.api.tx.payroll.Tx4600Request;
import payment.api.tx.payroll.Tx4600Response;
import payment.api.util.GUIDGenerator;

import java.util.*;

/**
 * 上传影印文件请求
 *
 * @Author wangyadong
 * @Date 2023/10/25 11:49
 * @ClassName CpcnTx4600Request
 */
public class CpcnTx4600Request implements CpcnPayRequest<CpcnTx4600Response> {
    private CpcnObject bizModel = null;

    @Override
    public String getTxCode() {
        return "4600";
    }

    @Override
    public String cpcnGatewayUrl() {
        return Gateway4FileEnvironment.GATEWAY4FILE_URL;
    }

    @Override
    public Class<CpcnTx4600Response> getResponseClass() {
        return CpcnTx4600Response.class;
    }

    @Override
    public CpcnObject getBizModel() {
        return this.bizModel;
    }

    @Override
    public void setBizModel(CpcnObject bizModel) {
        this.bizModel = bizModel;
    }

    /**
     * 业务托管
     *
     * @return
     */
    @Override
    public JSONObject getBusinessBiz() {
        if (ObjectUtils.isNotNull(bizModel)) {
            Tx4600Request tx4600Request = CopyObjectUtils.copyAtoB(bizModel, Tx4600Request.class);
            try {
                tx4600Request.process();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            return (JSONObject) JSONObject.toJSON(tx4600Request);
        }
        return null;
    }

    @Override
    public JSONObject verifier(String responseMessage, String responseSignature) {
        try {
            Tx4600Response tx4600Response = new Tx4600Response(responseMessage, responseSignature);
            return (JSONObject) JSONObject.toJSON(tx4600Response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) throws Exception {
        CpcnClient ccbClient = new CpcnAbstractClient("008734");
        CpcnTx4600Request request = new CpcnTx4600Request();
        CpcnTx4600Model model = new CpcnTx4600Model();
        model.setTxSN(GUIDGenerator.genGUID());
        model.setBusinessType("10");
        model.setInstitutionID("008734");

        Map<String,String> certMap = new HashMap<>();
        //30=个体工商营业执照
        certMap.put("30","D:\\photo\\22.png");
        //10=身份证人像面
        certMap.put("10","D:\\photo\\Snipaste_2022-08-15_15-14-13.png");
        //11=身份证国徽面
        certMap.put("11","D:\\photo\\Snipaste_2022-08-15_15-15-28.png");
        List<CpcnTx4600Model.ImageInfo> imageInfos = new ArrayList<>();
        Iterator<Map.Entry<String, String>> iterator = certMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> next = iterator.next();
            CpcnTx4600Model.ImageInfo imageInfo = new CpcnTx4600Model.ImageInfo();
            imageInfo.setImageType(next.getKey());
            imageInfo.setImageContent(ImageBase64Utils.localImageToBase64(next.getValue()));
            imageInfo.setItemNo(GUIDGenerator.genGUID());
            imageInfos.add(imageInfo);
        }
        model.setImageInfoList(imageInfos);
        request.setBizModel(model);
        CpcnTx4600Response execute = ccbClient.execute(request);
        System.out.println(JSONObject.toJSONString(execute));
    }
}
